package com.situ.struts.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Date;

public class FileUploadAction extends ActionSupport  {
    private String name;
    private Date birthday;

    //<input type="file" name="fileUpload">
    private File fileUpload;
    // 文件名：要求前面是fileUpload，后面是FileName
    private String fileUploadFileName;
    // 文件类型：要求前面是fileUpload，后面是ContentType
    private String getFileUploadContentType;

    @Override
    public String execute() throws Exception {
        System.out.println("execute().");
        System.out.println(name);

        // 文件上产
        ServletContext servletContext = ServletActionContext.getServletContext();
        String path = servletContext.getRealPath("/upload");
        // 在upload文件夹下面创建 一个名字叫fileUploadFileName文件
        File file = new File(path, fileUploadFileName);
        // 将上传的文件拷贝到目标文件夹中
        FileUtils.copyFile(fileUpload, file);
        return  SUCCESS;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public String getGetFileUploadContentType() {
        return getFileUploadContentType;
    }

    public void setGetFileUploadContentType(String getFileUploadContentType) {
        this.getFileUploadContentType = getFileUploadContentType;
    }
}
